package panda.server;

import panda.buffer.BufferManager;
import panda.file.FileManager;
import panda.log.LogManager;
import panda.metainfo.MetaInfoManager;
import panda.planner.Planner;
import panda.planner.impl.DPQueryPlanner;
import panda.planner.impl.NormalUpdatePlanner;
import panda.transaction.Transaction;

public class Panda {
	private static int BUFFER_SIZE = 128;
	
	private static FileManager fileManager;
	private static MetaInfoManager miManager;
	private static BufferManager bufferManager;
	private static Planner planner;
	private static LogManager logManager;
//	private static Transaction transaction;
	
	public static void init(String rootDirName){
		fileManager = new FileManager(rootDirName);
		bufferManager    = new BufferManager(BUFFER_SIZE);
		logManager  = new LogManager("panda.log");
		planner = new Planner(new DPQueryPlanner(), new NormalUpdatePlanner());
		Transaction transaction = new Transaction();
		boolean isnew = fileManager.getDirectory().isnew();
		miManager = new MetaInfoManager(isnew, transaction);
		if (isnew)
			System.out.println("creating new database");
		else {
			System.out.println("recovering existing database");
			transaction.recover();
		}
		transaction.commit();
	}
	
	public static FileManager getFileManager(){
		 return fileManager;
	}
	
	public static MetaInfoManager getMetaInfoManager() {
		return miManager;
	}
	
	public static LogManager getLogManager(){
		return logManager;
	}
	
	public static BufferManager getBufferManager() {
		return bufferManager;
	}
	
	public static Planner getPlanner() {
		return planner;
	}
}
